﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using F1010DB.Constant;
using F1010DB.CustomEntity.InitDanhMuc;

namespace F1010DB.Common
{
    public abstract class AbstractClassLoadData : AbstractGetBridgeConnect
    {
        private const string SELECT_TEMPLATE = " SELECT * FROM {0}";
        private const string SQL_GROUP_BASICAL = "SELECT MA_CO_BAN, TEN_DM_CO_BAN FROM HDVNSYS.DANH_MUC_CO_BAN GROUP BY MA_CO_BAN,TEN_DM_CO_BAN ORDER BY MA_CO_BAN DESC";
        private string[] groupSQLTable;
        private string[] sqlString;
        private string tableName;
        private DataTable dtG;
        //private Dictionary<>
        protected abstract string[] getSqlStringToSelect();

        private void initString()
        {
            groupSQLTable = getSqlStringToSelect();
            sqlString = new string[groupSQLTable.Length];

            for (int i = 0; i< groupSQLTable.Length; i++)
            {
                tableName = groupSQLTable[i];
                sqlString[i] = String.Format(SELECT_TEMPLATE, tableName);
            }
        }
        
        /// <summary>
        /// Trường hợp các danh mục thuộc nhóm cơ bản không xử lý chỉ tham khảo
        /// </summary>
        protected void initStringForBasical()
        {
            DataTable dtGroup = dbContext.readATable(SQL_GROUP_BASICAL, CommandType.Text, null);

            if (dtGroup != null || dtGroup.Rows.Count !=0 )
            {
                dtG = dtGroup;
                InitDisplayLoad.dicBasical = new Dictionary<string, string>();

                foreach(DataRow dr in dtG.Rows)
                {
                    InitDisplayLoad.dicBasical.Add(dr[0].ToString(), dr[1].ToString());
                }
            }
        }

        protected Dictionary<string, DataTable> listGetData()
        {
            initString();

            Dictionary<string, DataTable> listDanhMuc = new Dictionary<string, DataTable>();

            for (int i = 0; i < sqlString.Length; i++)
            {
                string key = groupSQLTable[i];
                DataTable dt = dbContext.readATable(sqlString[i], CommandType.Text, null);
                listDanhMuc.Add(key, dt);
            }

            // Thêm vào danh mục cơ bản
            if (InitDisplayLoad.dicBasical != null || InitDisplayLoad.dicBasical.Count != 0)
            {
                string temp = "SELECT * FROM HDVNSYS.DANH_MUC_CO_BAN WHERE MA_CO_BAN = '{0}'";

                foreach (string key1 in InitDisplayLoad.dicBasical.Keys)
                {
                    string sql1 = String.Format(temp, key1);
                    DataTable dtA = dbContext.readATable(sql1, CommandType.Text, null);
                    listDanhMuc.Add(key1, dtA);
                }
            }

            return listDanhMuc;
        }

        protected Dictionary<string, string> listMessages()
        {
            string SqlMessage = " SELECT * FROM HDVNSYS.MESSAGES_FOR_APP";
            DataTable dt = dbContext.readATable(SqlMessage, CommandType.Text, null);
            Dictionary<string, string> msgDict = new Dictionary<string, string>();

            foreach(DataRow dr in dt.Rows)
            {
                string mshId = dr["MSG_ID"].ToString();
                string mshContent = dr["MSG_CONTENT"].ToString();

                msgDict.Add(mshId, mshContent);
            }

            return msgDict;
        }
    }
}
